home *** CD-ROM | disk | FTP | other *** search
- 25-Minute Workout
- Part I: Questions
- Just to make sure that you were paying attention, I want to ask you a few questions. ôYou
- vill answer!ö (The answers are on the disk. The answer files are named according to Part
- number.)
- 1
- Declare the following. (In function declarations, assume that the functions take no
- arguments.)
- a. An array of ten integers
- b. A pointer to an integer
- c. An array of ten pointers to integers
- d. A function returning a pointer to an integer
- e. A pointer to a function that takes an integer and returns nothing
- 2
- Given that the function fn() takes an int followed by a float and returns an int, why will
- the following not work?
- void anotherFn()
- {
- int x;
- x = fn(1, 2);
- }
- What can you do to make it work? Hint: DonÆt change the call in this prototypical
- problem.
- 3
- WhatÆs wrong with the following displayArray() function?
- #include <stdio.h>
- void displayArray(int *pArray, int number)
- {
- /*arrange in two columns*/
- int i;
- int noPairs = number / 2;
- int odd = number % 2;
- for (i = 0; i < (2 * noPairs);)
- {
- printf(ôarray[%2d] = %d, array[%2d] = %d\nö,
- i, pArray[i++], i, pArray[i++]);
- }
- if (odd)
- {
- printf(ôarray[%2d] = %d\nö, i, pArray[i]);
- }
- }
-
- int main()
- {
- int array[] = {1, 2, 3, 4, 5};
-
- displayArray(array, 5); /*may not generate proper
- output*/
- return 0;
- }
- 4
- Write and prototype a function that accepts from the keyboard two integer values, x and y,
- and returns their values in arguments passed from the calling function.
- 5
- Assuming that you want to know the length in bytes of a particular structure, whatÆs
- wrong with the following?
- /*function to return size of a MyStruct*/
- int sizeOfMyStruct()
- {
- /*allocate an array of 2 MyStructs*/
- struct MyStruct ms[2];
-
- /*now return the difference of the address of
- the second element minus that of the first*/
- return &ms[1] - &ms[0];
- }
- Part II: Questions
- Now weÆve come to my favorite part of every part, the workout. Slip on your leotards and
- get out your keyboards. YouÆll have 25 minutes and no cheating.
- 1
- a. Given the following macro, list any potential problems you can find
- with it:
- //TWO_MAX - return 2 * maximum of two numbers
- #define TWO_MAX (x, y) x > y ? (2 * x) : (2 * y)
- b. How can these problems best be avoided?
- 2
- C++ doesnÆt allow arguments other than the last to be given default values. Thus, the
- following is wrong:
- void aFunc(int x, float y = 0.0, char z) {
- cout << ôx = ô << x
- << ô, y = ô << y
- << ô, z = ô << z
- << ô\nö;
- }
- Is there some way we can achieve the result we want in this example? (DonÆt overload
- your brain thinking about this one.)
- 3
- Which of the following overloaded functions are called in each of the calls in main()? If
- any of the calls are ambiguous (that is, they canÆt be resolved), list all candidates:
- void fn(int x) {} //1
- void fn(char *x) {} //2
- void fn(void *x) {} //3
- void fn(int x, float y) {} //4
- void fn(int x, double y) {} //5
- int main()
- {
- fn(1); //a
- fn(ô1ö); //b
- int i;
- fn(&i); //c
- fn(æ1Æ ); //d
- fn(1, 2); //e
- return 0;
- }
- 4
- The following program compiles but does not link properly. Why?
- int printf(const char *, ...);
- int main()
- {
- printf(ôHello world\nö);
- return 0;
- }
- I mentioned the solution in the text, but donÆt go back and look until youÆve thought
- about it.
- 5
- In Part I, you were asked to write and prototype a function that accepts two integer
- values, x and y, from the keyboard and returns them in arguments passed from the calling
- function. Do it again, but this time use referential arguments. Solve the problem twice,
- the first time using scanf() and the second time using stream input.
- 6
- Let us assume the existence of a new mathematical class called Davis. (IÆll be
- immortalized.) All the operations defined on real numbers are defined also on Davis.
- Write the prototypes for a set of overloaded functions called multiply() that perform
- multiplication between Davis numbers and Davis numbers as well as between Davis
- numbers and floating-point numbers. (Note that a Davis times a Davis returns a Davis,
- and a Davis times a float returns a Davis.)
- 7
- The first program that anyone writes in C is the following ôHello worldö program:
- #include <stdio.h>
- int main()
- {
- printf(ôHello, world\nö);
- return 0;
- }
- Rewrite this program to use stream output.
- Part III: Questions
- This part has only one exercise. The time has come to sink or swim. Write a program that
- does the following.
- Model a set of courses and students. Students have names. Students can enroll in courses.
- Students are given a grade (between 0 and 4) in each class. Courses can have different
- numbers of semester hours.
- After all the students have been graded, the program should then spit out the following:
- * The average grade in each course
- * Each studentÆs grade in each course
- * The studentÆs average grade for all classes
- * The studentÆs grade in each course curved by the average of all grades
- in the class
- Write your solution in C++. (I didnÆt really need to say that, did I?)
- Here are a few tips for solving this problem. One, use an object-oriented approach.
- Protect your data members as much as possible.
- Two, study the requirements to find the candidate classes. Then examine the output
- requirements to determine the member functions you will need. Make CRC cards if it
- helps.
- Three, use the following simple curving formula:
- curved grade = 3.0F * (actual grade / average)
- This makes the average grade 3.0. Make sure, however, that the grade of any single
- student does not exceed 4.0.
- Four, concentrate on what classes you need to solve this problem. To keep the program as
- short as possible, you can hardcode the actual course objects and student objects that you
- will need to test your classes. Just make sure that you provide enough courses and
- students to show that the classes work.
- Part IV: Questions
- Once again, itÆs time to start sweating. Get out your keyboards and put away your notes.
- You have 25 minutes @md and no cheating.
- 1
- a. Write a constructor for the class Student in the program you wrote for
- the Part III Workout. (You did write that program, didnÆt you?) This
- constructor should not make any restrictions on the length of the name.
- Hint: Use new.
- b. Consider the following class:
- class Motor
- {
- public:
- Motor(int noCylinders, float displacement);
- };
- class Car
- {
- public:
-
- //this constructor is not complete
- Car(int color, float displacement,
- int noDoors = 4, int noCylinders = 6)
- {
- //stuff goes here
- }
- protected:
- Motor motor;
- int paintColor;
- int numberDoors;
- };
- Complete the constructor for Car. DonÆt forget to initialize the member
- motor.
- c. In the following declaration of student, what constructor call is invoked
- on which members?
- Student otherStudent(ôDannyö);
- Student student[5] = {ôRandyö, otherStudent, ôTrudyö};
- d. What does the following do?
- class Student
- {
- public:
- Student(char *pName);
- Student()
- {
- Student("no name");
- }
- };
- e. Assuming that the intent in 1d was to avoid having two constructors,
- how could you do this correctly? There are several ways to do this.
-
- 2
- a. Why is the constructor Student::Student(Student) illegal? Hint: In older
- versions of the compiler in which the constructor is not illegal,
- invoking this constructor is always fatal.
- b. Why was the copy constructor in BUDGET4 declared protected? Hint:
- What asset is it trying to protect?
- 3
- a. WhatÆs wrong with the following count() function? Hint: This is a
- subtle problem.
- class Student
- {
- public:
- static int count;
- };
- int Student::count;
-
- //count - count how many students between first and last
- void count(Student *pFirst, Student *pLast)
- {
- Student *pS = pFirst;
- pS->count = 0; //start with 0
- while (pS != pLast) //until we get to the last one
- {
- pS++->count += 1; //count each one
- } //leave count with student count
- }
- b. How many bytes of memory are used by s? (Assume that a char is one
- byte and that an int is two bytes.)
- class Student
- {
- public:
- char name[40];
- static int count;
- };
- Student s[10];
- Part V: Questions
- ItÆs review time again. (Dawg!) Set that calisthenics timer and see whether you know as
- much as you think you do. As always, I have provided answers in case you get stuck.
- 1
- a. In BUDGET5, both account types (Checking and Savings) use the same
- display function. In previous versions of BUDGET, however, savings
- accounts display the number of withdrawals. Make the savings
- accounts do so again. Hint: Use virtual functions and make minimal
- changes to the classes.
- b. The bank wants a new Savings account that acts just like a conventional
- Savings account if the balance is below $750. If the balance stays at or
- above that magical number, however, the bank wonÆt charge a fee.
- Without changing the existing classes, add a new class to BUDGET5 to
- handle this change.
- c. BUDGET5 also displays Savings and Checking accounts mixed
- together. Separate the two types of accounts while they are displayed,
- as they were in previous versions of BUDGET. Hint: There are two
- approaches here. One is to keep the two accounts in separate lists. The
- second is to recognize which is which when theyÆre in the same list.
- Try both approaches.
-
- 2
- Factor the following concrete classes into some type of hierarchy. You will want to create
- new classes to represent abstract concepts.
- Citroen CV2 (small air-cooled 2-door)
- Sedan
- Pickup
- Station wagon
- Panel truck
- 18-wheeler
-